#include "stdio.h"
#include "s_sample.h"
#include "types.h"
//-----------------------------------------------
#define LOG(...)                        printf( __VA_ARGS__)
userAdc_t   userAdcStruct;
Weight_t ScaleWeight;
//-----------------------------------------------
uint32_t medianFilter(const uint32_t* buff,uint8_t buffLen)
{
    uint32_t FindFlag = 0;
    uint32_t MidValue = 0;
    uint8_t mackIndex = 0;
    uint8_t j = 0;
    for(uint8_t i = 0;i < ((buffLen+1)>>1) ; i++)
    {
        MidValue = 0;
        MidValue --;      //undata max
        for( j = 0;j < buffLen ; j++)
        {
            if(!((FindFlag>>j)&0x01) && (MidValue >= buff[j]))
            {
                MidValue = buff[j];
                mackIndex = j;
            }
        }
        FindFlag |= (0x01 << mackIndex);
    }
    return MidValue;
}

/**
 * @brief  去除最大最小值求平均值
 * @param  buff         数组指针
 * @param  buffLen      数组长度，长度不能大于32
 * @return uint32_t     返回查找到数组中位值
 */
uint32_t WithoutMinMaxAverage(const uint32_t* buff,uint8_t buffLen)
{
    uint64_t SumAdc = 0;
    uint32_t BigAdc = 0;
    uint32_t MinAdc = 0;
    MinAdc --;                  //Unit max
    for(uint8_t i = 0;i < buffLen ; i++)
    {
        if(MinAdc >  buff[i])
            MinAdc = buff[i];
        if(BigAdc < buff[i])
            BigAdc = buff[i];
        SumAdc += buff[i];
    }
    SumAdc -= MinAdc;
    SumAdc -= BigAdc;
    return SumAdc/(buffLen-2);
}

Weight_t ScaleWeight={
	.Flag.Byte = 0,
};
WaterFlow_t  waterFlow;
#define      OUT_RANGE_WATER_MinD           1           //0.5g
#define      IN_RANGE_WATER_TIMED           8           //0.5*8   = 4g
#define      OUT_RANGE_WATER_TIMED          100         //100*0.5 = 50g
void  F_DetectiveWaterFlow(void)
{
    int32_t subAdc = 0;
    uint8_t temp = ADD_WATER;
    if(!userAdcStruct.Flag.Bit.SampleNew)
    {
        return;
    }
    subAdc = waterFlow.lastFiltAdc - userAdcStruct.FilterAdc;
    waterFlow.Flag.Bit.StopTrig = false;
    LOG("waterFadc=%d\r\n",userAdcStruct.FilterAdc);
    if(subAdc < 0)
    {
        temp = SUB_WATER;
        subAdc = 0 - subAdc;
    }
    if(subAdc > ScaleWeight.DivisionAdc)        //0.5g
    {
        if(subAdc > ScaleWeight.DivisionAdc*OUT_RANGE_WATER_TIMED)      //rash weight over range not water
        {
            if(!waterFlow.Flag.Bit.WaterFlowIng)
                waterFlow.Flag.Bit.StopTrig = 1;
        }else{                                                          //weight change in range
            if(temp != waterFlow.WaterType)
                waterFlow.Flag.Bit.WaterFlowRest = true;
            else{
                if(subAdc > ScaleWeight.DivisionAdc*IN_RANGE_WATER_TIMED)   //中水流计数
                {
                    waterFlow.MiddleCnt ++;
                    if(waterFlow.MiddleCnt > 5){
                        waterFlow.Flag.Bit.WaterFlowIng = true;             //水流启动
                        LOG("midle water flow ing\r\n");
                    }
                }else{                                                      //小水流计数
                    waterFlow.litterCnt++;
                    if(waterFlow.litterCnt > 2)
                        waterFlow.MiddleCnt = 0;
                    if(waterFlow.litterCnt > 3)
                    {
                        waterFlow.Flag.Bit.WaterFlowIng = true;             //水流启动
                        LOG("litter water flow ing\r\n");
                    }
                }                
            }
            waterFlow.WaterType = temp;
        }        
    }else{
        waterFlow.Flag.Bit.StopTrig = 1;
    }
    if(waterFlow.Flag.Bit.StopTrig)
    {
        waterFlow.stopCnt++;
        if(waterFlow.stopCnt > 5)
        {
            waterFlow.Flag.Bit.WaterFlowRest = true;
            waterFlow.WaterType = 0;
        }
    }
    if(waterFlow.Flag.Bit.WaterFlowRest)
    {
        waterFlow.Flag.Byte = 0;
        waterFlow.stopCnt = 0;
    }
    waterFlow.lastFiltAdc = userAdcStruct.FilterAdc;
}


uint32_t const adcBuffer[]={
8680217,
8680228,
8680239,
8680242,
8680244,
8680243,
8680239,
8680236,
8680234,
8680233,
8680235,
8680236,
8680228,
8680217,
8680205,
8680192,
8680194,
8680194,
8680194,
8680193,
8680190,
8680188,
8680190,
8680193,
8680207,
8680231,
8680257,
8680287,
8680313,
8680342,
8682296,
8685058,
8687807,
8690548,
8693279,
8695996,
8698717,
8701438,
8704159,
8706875,
8709582,
8710366,
8710368,
8710339,
8710328,
8710326,
8710327,
8710335,
8710341,
8710342,
8710333,
8710324,
8710325,
8710326,
8710332,
8710338,
8710334,
8710333,
8710322,
8710314,
8710310,
8710314,
8710317,
8710320,
8710321,
8710313,
8710305,
8710295,
8710286,
8710293,
8710297,
8710301,
8710300,
8710291,
8710282,
8710282,
8710289,
8710294,
8710302,
8710304,
8710305,
8710312,
8710312,
8710315,
8710325,
8710328,
8710335,
8710338,
8710333,
8710326,
8710323,
8710325,
8710333,
8710341,
8710347,
8710347,
8710336,
8710327,
8710331,
8710335,
8710341,
8710353,
8710359,
8710363,
8710360,
8710356,
8710355,
8710354,
8710362,
8710362,
8710362,
8710355,
8710344,
8710344,
8710344,
8710347,
8710348,
8710352,
8710350,
8710347,
8710341,
8710344,
8710355,
8710363,
8710370,
8710372,
8710365,
8710353,
8710344,
8710345,
8710348,
8710352,
8710349,
8710352,
8710343,
8710337,
8710337,
8710341,
8710347,
8710356,
8710358,
8710357,
8710350,
8710343,
8710345,
8710355,
8710363,
8710362,
8710362,
8710354,
8710344,
8710342,
8710343,
8710352,
8710359,
8710368,
8710370,
8710368,
8710358,
8710352,
8710359,
8710362,
8710371,
8710372,
8710368,
8710363,
8710356,
8710356,
8710357,
8710366,
8710608,
8710870,
8711058,
8711382,
8711750,
8711991,
8712125,
8712170,
8712169,
8712170,
8712584,
8712993,
8713371,
8713469,
8713599,
8713744,
8713534,
8713166,
8712972,
8712890,
8712903,
8712963,
8712969,
8712602,
8712032,
8711613,
8711269,
8710983,
8710918,
8710910,
8710907,
8710901,
8710894,
8710876,
8710862,
8710855,
8710845,
8710845,
8710845,
8710841,
8710831,
8710821,
8710814,
8710815,
8710821,
8710830,
8710837,
8710840,
8710834,
8710833,
8710821,
8710821,
8710817,
8710823,
8710825,
8710821,
8710810,
8710809,
8710805,
8710806,
8710808,
8710815,
8710851,
8710923,
8711024,
8711168,
8711353,
8711601,
8711903,
8712234,
8712596,
8713010,
8713473,
8713931,
8714374,
8714825,
8715300,
8715771,
8716220,
8716643,
8717068,
8717518,
8717971,
8718417,
8718868,
8719336,
8719792,
8720245,
8720701,
8721167,
8721648,
8722155,
8722655,
8723145,
8723635,
8724160,
8724688,
8725267,
8725845,
8726427,
8727021,
8727623,
8728218,
8728834,
8729466,
8730119,
8730760,
8731403,
8732013,
8732596,
8733200,
8733809,
8734421,
8735038,
8735631,
8736228,
8736841,
8737500,
8738178,
8738858,
8739596,
8740383,
8741212,
8742058,
8742917,
8743814,
8744705,
8745525,
8746245,
8746899,
8747498,
8748011,
8748398,
8748667,
8748880,
8749027,
8749091,
8749108,
8749105,
8749084,
8749074,
8749063,
8749065,
8749070,
8749075,
8749080,
8749080,
8749079,
8749071,
8749067,
8749070,
8749077,
8749087,
8749092,
8749085,
8749067,
8749057,
8749055,
8749059,
8749063,
8749071,
8749073,
8749068,
8749060,
8749056,
8749046,
8749049,
8749067,
8749076,
8749083,
8749080,
8749078,
8749091,
8749095,
8749110,
8749124,
8749139,
8749142,
8749143,
8749140,
8749140,
8749152,
8749160,
8749169,
8749174,
8749169,
8749157,
8749151,
8749148,
8749148,
8749156,
8749156,
8749152,
8749149,
8749135,
8749131,
8749131,
};


